<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <a-button
          v-btn-permission="'lcm_003_add_01'"
          type="primary"
          icon="plus"
          @click="visible = true"
        >
          新增计划外招标
        </a-button>
        <div
          class="Statistics"
          style="
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
            margin-top: 10px;
            background: #e6f3fc;
            position: relative;
          "
        >
          <a-icon
            style="color: #108ee9"
            type="exclamation-circle"
          />
          <span style="margin: 0 10px">已选择{{ count }}项</span>
          <span style="margin: 0 10px">总计：</span>
          <span style="margin: 0 10px">计划招标总数:{{ summaryObj.plannedBiddingCount }}</span>
          <span style="margin: 0 10px">计划外招标总数: {{ summaryObj.plannedOutBiddingCount }}</span>
          <span style="margin: 0 10px">计划完成数:{{ summaryObj.plannedCompletion }}</span>
          <span style="margin: 0 10px">计划未完成数:{{ summaryObj.plannedNoCompletion }}</span>
          <div style="display: inline-block; position: absolute; right: 10px; top: 5px">
            <a-button
              type="primary"
              style="margin-top: -5px"
              @click="getSummary"
            >
              汇总选中
            </a-button>
            <a-button
              type="primary"
              @click="getAll"
            >
              汇总所有
            </a-button>
            <a
              v-show="summaryTable"
              style="margin-left: 8px"
              @click="getSummaryData"
            >
              <span>展开详细汇总信息</span>
              <a-icon type="down" />
            </a>
          </div>
        </div>
      </div> -->

      <list-table ref="table" :columns="columns" :request="loadData" :out-selected-row-keys="selectedRowKeys" :statistics-config="{ use: true, statistics, temp }" :exportConfig="{ use: true }">
        <template slot="prefix" slot-scope="ref">
          <a-button v-btnPermission="'lcm_003_add_01'" type="primary" icon="plus" @click="handleEdit(null, 1)"> 新增计划外招标 </a-button>
          <a-button type="primary" style="margin-top: -5px" @click="getSummary"> 汇总选中 </a-button>
          <a-button type="primary" @click="getAll"> 汇总所有 </a-button>
          <a v-show="summaryTable" style="margin-left: 8px" @click="getSummaryData">
            <span>展开详细汇总信息</span>
            <a-icon type="down" />
          </a>
        </template>
        <template slot="status" slot-scope="text, record, index">
          <list-status :status="transfer(text)" :text="text" />
        </template>
        <template slot="expandedRowRender" slot-scope="record" style="margin: 0">
          <span style="display: block">
            <span>计划合同签订时间：</span>
            <span>{{ record.planContractTime }}</span>
          </span>
          <span style="display: block">
            <span>分包工程招标内容：</span>
            <span>{{ record.biddingContent }}</span>
          </span>
          <span style="display: block">
            <span>计划完工时间：</span>
            <span>{{ record.planEndTime }}</span>
          </span>
        </template>
        <template slot="action" slot-scope="text, record">
          <MoreButton>
            <a @click="getDetails(record)">查看</a>
            <a v-if="record.biddingSituation === '计划外招标' && record.status === '待提交审核'" @click="handleDel(record.id)">删除</a>
            <a
              v-if="record.biddingSituation !== '计划外招标' && record.status !== '审核中'"
              @click="
                recordId = record.id
                carryoutVisible = true
              "
              >录入完成情况</a
            >
            <template v-if="record.status === '审核通过' || record.status === '审核中' || record.status === '审核不通过'">
              <approval-button v-if="record.biddingSituation !== '计划外招标'" :id="record.id" />
            </template>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
    <!--eslint-disable      -->
    <summary-data
      :summaryVisible="summaryVisible"
      :selectCount="count"
      :summaryObj="selectSummaryObj"
      :loading="confirmLoading"
      @ok="() => (this.summaryVisible = false)"
      @cancel="() => (this.summaryVisible = false)"
    ></summary-data>
    <add-post :visible.sync="visible" :form="chooseRow" @ok="$refs.table.refresh(true)" />
    <carryout :visible.sync="carryoutVisible" :id="recordId" @ok="$refs.table.refresh(true)"></carryout>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components'
import {
  getTechnologyLinkage,
  getTechnologyProject,
  editBranchBiddingComplete,
  biddingCompleteSummary,
  deleteBySubContractPlanOut
  // findListCount2
} from '@/api/legal'
import { getDropDown2 } from '@/api/dropDown'
import summaryData from '@/views/Legal/branchBidding/planComplete/summaryData'
import addPost from './addOutPlanBidding'
import carryout from '@/views/Legal/branchBidding/planComplete/carryout'

const columns = [
  {
    title: '所属分公司',
    dataIndex: 'branchCompany',
    ellipsis: true,
    width: 150
  },
  {
    title: '项目名称',
    dataIndex: 'projectName',
    ellipsis: true,
    width: 150
  },
  {
    title: '招标情况',
    dataIndex: 'biddingSituation',
    ellipsis: true,
    width: 120
  },
  {
    title: '招标内容 ',
    dataIndex: 'biddingContent',
    ellipsis: true,
    width: 340
  },
  {
    title: '招标合同额（万元）',
    dataIndex: 'tenderContractMoney',
    ellipsis: true,
    width: 200
  },
  {
    title: '分包商选择方式',
    dataIndex: 'subcontractorsSelect',
    ellipsis: true,
    width: 140
  },
  {
    title: '分包方式',
    dataIndex: 'branchPackMode',
    ellipsis: true,
    width: 100
  },
  {
    title: '计划招议标时间',
    dataIndex: 'planBiddingTime',
    ellipsis: true,
    width: 130
  },
  {
    title: '计划开工时间',
    dataIndex: 'planTime',
    ellipsis: true,
    width: 130
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' },
    ellipsis: true,
    width: 150
  },
  {
    title: '操作',
    fixed: 'right',
    width: 220,
    scopedSlots: { customRender: 'action' }
  }
]
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
export default {
  name: 'a' + Date.now(),
  components: {
    STable,
    summaryData,
    addPost,
    carryout
  },
  data() {
    this.columns = columns
    return {
      searchArr: [
        // 搜索框
        {
          name: '所属分公司',
          valueKey: 'branchCompany',
          type: SelectSubCompany
        },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectName',
        //   keyValue: 'branchCompany'
        // },
        {
          name: '计划开工时间',
          value: 'plan',
          type: 'dateRangeFormat'
        },
        {
          name: '计划招标内容',
          value: 'branchProjectName',
          type: 'input'
        },
        {
          name: '状态',
          value: 'status',
          type: 'select',
          data: [
            { label: '审批', value: '审批' },
            { label: '未审批', value: '未审批' }
          ]
        },
        {
          name: '计划招议标时间',
          value: 'planBidding',
          type: 'dateRangeFormat'
        },
        {
          name: '分包方式',
          value: 'branchPackMode',
          type: 'select',
          data: [],
          request: () => getDropDown2({ query: { type: 0 } })
        },
        {
          name: '分包商选择方式',
          value: 'subcontractorsSelect',
          type: 'select',
          data: [],
          request: () => getDropDown2({ query: { type: 1 } })
        },
        {
          name: '招标合同额（万元）',
          value: 'tenderContractMoney',
          type: 'rangeInput'
        },
        {
          name: '招标情况',
          value: 'biddingSituation',
          type: 'select',
          data: [
            { label: '待招标', value: '待招标' },
            { label: '完成招标', value: '完成招标' },
            { label: '未按计划招标(有说明)', value: '未按计划招标(有说明)' },
            { label: '未按计划招标(无说明)', value: '未按计划招标(无说明)' },
            { label: '计划外招标', value: '计划外招标' }
          ]
        }
      ],
      queryParam: {},
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        if (requestParameters.planTime) {
          delete requestParameters.planBiddingStartTime
          delete requestParameters.planBiddingEndTime
        }
        if (requestParameters.planBidding) {
          delete requestParameters.planBiddingStart
          delete requestParameters.planBiddingEnd
        }
        if (requestParameters.planTime) {
          requestParameters.planBiddingStartTime = this.formatDate(requestParameters.planTime[0])
          requestParameters.planBiddingEndTime = this.formatDate(requestParameters.planTime[1])
        }
        if (requestParameters.planBidding) {
          requestParameters.planBiddingStart = this.formatDate(requestParameters.planBidding[0])
          requestParameters.planBiddingEnd = this.formatDate(requestParameters.planBidding[1])
        }
        return editBranchBiddingComplete(requestParameters).then((res) => {
          this.totalSize = res.totalSize
          return res
        })
      },
      temp: () => {
        return (
          <span>
            计划招标总数:{this.summaryObj.plannedBiddingCount || 0}， 计划外招标总数:{this.summaryObj.plannedOutBiddingCount || 0}， 计划完成数: {this.summaryObj.plannedCompletion || 0}， 计划未完成数:
            {this.summaryObj.plannedNoCompletion || 0}，
          </span>
        )
      },
      selectedRows: [],
      statistics: {},

      branchCompanyList: [],
      projectList: [],
      summaryObj: {
        plannedBiddingCount: undefined,
        plannedOutBiddingCount: undefined,
        plannedCompletion: undefined,
        plannedNoCompletion: undefined,
        oldSearchData: {},
        idArr: []
      },
      summaryVisible: false,
      confirmLoading: false,
      summaryTable: false,
      idArr: [],

      visible: false,
      carryoutVisible: false,
      recordId: null,
      chooseRow: null,

      totalSize: undefined, //总条数
      count: undefined,
      selectSummaryObj: {},
      selectedRowKeys: []
    }
  },
  filters: {
    statusFilter(type) {
      return statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type].status
    }
  },
  created() {
    getTechnologyLinkage().then((res) => {
      this.branchCompanyList = res.data
    })
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        selectedRows: this.selectedRows, // 当前行所有数据
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleDel(id) {
      let params = id ? [id] : this.selectId
      this.$delList2(
        deleteBySubContractPlanOut,
        () => {
          this.$refs.table.refresh(true)
        },
        { ids: params }
      )
    },
    transfer(status) {
      switch (status) {
        case '待提交审核':
          return 'DRAFT'
        case '审核中':
          return 'SUBMITTED'
        case '审核通过':
          return 'PAST'
        case '审核不通过':
          return 'REJECTED'
      }
    },
    search(val) {
      this.queryParam = val
      this.$refs.table.refresh(true)
    },
    getProject(id) {
      getTechnologyProject({ id: id }).then((res) => {
        this.projectList = res.data
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.count = selectedRowKeys.length
    },

    getPlanDiscussionTime(data, dateString) {
      if (dateString.length === 2) {
        this.queryParam.planBiddingStartTime = dateString[0]
        this.queryParam.planBiddingEndTime = dateString[1]
      }
    },
    getSummary() {
      this.selectedRowKeys = this.$refs.table.selectedRowKeys
      //汇总
      this.idArr = []
      if (!this.selectedRowKeys) {
        this.$message.info('请在表格中选择索要汇总的信息')
        return
      }
      this.idArr = this.selectedRowKeys.join(',')
      let search = {
        idArr: this.idArr,
        contentType: 1
      }
      this.selectSummaryObj = search
      this.selectSummaryObj.count = this.count
      biddingCompleteSummary(search).then((res) => {
        if (res.code === 200) {
          this.summaryObj = res.data
          this.summaryTable = true
          this.summaryObj.idArr = this.idArr
          this.summaryObj.oldSearchData = {}
        } else {
          this.summaryTable = false
          this.$message.error('汇总失败')
        }
      })
    },
    getAll() {
      let search = Object.assign({}, this.oldSearchData)
      search.idArr = []
      search.contentType = 1
      this.selectSummaryObj = search
      this.selectSummaryObj.count = this.totalSize
      biddingCompleteSummary(search).then((res) => {
        if (res.code === 200) {
          this.summaryObj = res.data
          this.count = this.totalSize
          this.summaryTable = true
          this.summaryObj.oldSearchData = this.oldSearchData
          this.summaryObj.idArr = []
        } else {
          this.summaryTable = false
          this.$message.error('汇总失败')
        }
      })
    },
    getSummaryData() {
      this.summaryVisible = true
    },
    getDetails(record) {
      this.$router.push({
        path: '/legal/branch-bidding/details',
        query: {
          id: record.id
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .excelBtnAll {
  display: none;
}
/deep/ .excelBtn {
  display: none;
}
</style>
